﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MMMaze
{
    public class RoomMatrix3D<TWallContent>:IList<Area3D <TWallContent>>
    {


        public RoomMatrix3D(Area3D<TWallContent> rootitem)
        {
            if (rootitem == null)
            {
                throw new ArgumentException("root area can not be null");
            
            }
            Core.Add(rootitem);
        }
        public  Area3D<TWallContent> Root
        {
            get
            {
                return this[0];
            }
            private   set
            {

                this[0]=value ;
            }
        }

        public void AddRoom(Area3D<TWallContent> item, Area3D<TWallContent>  neighbor,Axises3D axis,AxisDirection direction)
        {
            if (this.Contains(neighbor))
            {
                Core.Add(item);
                var oldv = neighbor.GetAxises(axis).GetNeighbor(direction); ;

                if (oldv != null)
                {
                    item.Axises.Clear();
                    item.Axises.AddRange(oldv.Axises);
                    if (oldv is IDisposable)
                    {
                        ((IDisposable)oldv).Dispose();

                    }

                }
                else
                {
                    neighbor.GetAxises(axis).SetNeighbor(direction, item);
                    item.GetAxises(axis).SetNeighbor((AxisDirection) (- (int)direction), neighbor);
                }
             
            }
            else
            {

                throw new IndexOutOfRangeException("no such member as neighbor");
            
            }
        }


        private List<Area3D <TWallContent>> Core= new  List<Area3D<TWallContent>>    (); 

        #region IList<Area3D<TWallContent>> Members

        public int IndexOf(Area3D<TWallContent> item)
        {
            return Core.IndexOf(item);
        }

        public void Insert(int index, Area3D<TWallContent> item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public Area3D<TWallContent> this[int index]
        {
            get
            {
                return Core[index];
            }
             set
            {
                if (value == null)
                {
                    this.RemoveAt(index);
                    return;
                }
                Core[index] = value;
                
            }
        }

        #endregion

        #region ICollection<Area3D<TWallContent>> Members

        public void Add(Area3D<TWallContent> item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            Core.Clear();
        }

        public bool Contains(Area3D<TWallContent> item)
        {
            return Core.Contains (item);
          
        }

        public void CopyTo(Area3D<TWallContent>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return Core.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(Area3D<TWallContent> item)
        {
            var rval= Core.Remove(item) ;
            
            if (item is IDisposable)
            {
                ((IDisposable)item).Dispose();
                
            }

            return rval;
            
        }

        #endregion

        #region IEnumerable<Area3D<TWallContent>> Members

        public IEnumerator<Area3D<TWallContent>> GetEnumerator()
        {
            return Core.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Core.GetEnumerator();
        }

        #endregion
    }
}
